package com.hnkywl.sts.dao.xtgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.xtgl.Czrz;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-8
 * Time: 下午4:06
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class CzrzDao extends SimpleHibernateTemplate<Czrz, Long> {
    public Page<Czrz> findCzrzList(Long yhId,String yhXm,Date czrq,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Czrz> page = new Page<Czrz>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (yhId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" yh.id = ? ");
            param.add(yhId);
        }
        if (yhXm != null && !"".equals(yhXm)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" yh.xm like ? ");
            param.add("%" + yhXm.trim() + "%");
        }
        if (czrq != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" (czrq >= ? ");
            condition.append(" and ");
            condition.append(" czrq < ?) ");
            param.add(czrq);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(czrq);
            calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
            Date czrqEnd=calendar.getTime();
            param.add(czrqEnd);
        }
        //==============记录数量===============
        hql = "select count(*) from Czrz where 1=1" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Czrz where 1=1" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }
}
